package com.daluzy.chmanager.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Comment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class CommentManageAdapter extends RecyclerView.Adapter<CommentManageAdapter.MyViewHolder> {


    private Context context;
    private List<Comment> myData = new ArrayList<>();

    public CommentManageAdapter(Context context, List<Comment> myData) {
        this.context = context;
        this.myData = myData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_comment_manage_item_daluzy, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Comment comment = myData.get(position);
        holder.tvCommentDetails.setText(comment.getComment_details());
        holder.tvCommentLevel.setText(comment.getComment_level());
        holder.tvCommentStudent.setText(comment.getComment_student());
        holder.tvCommentEmployee.setText(comment.getComment_employee());
        holder.tvTime.setText(comment.getCreatedAt());
        if (comment.getComment_status().equals("1")) {
            holder.tvCommentStatus.setText("未审核");
        } else if (comment.getComment_status().equals("2")) {
            holder.tvCommentStatus.setText("审核通过");
        } else {
            holder.tvCommentStatus.setText("审核失败");
        }

        holder.tvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateComment(comment,position,false);
            }
        });
        holder.tvYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateComment(comment,position,true);
            }
        });
    }

    private void updateComment(Comment comment, int position, boolean yn) {
        //更新Person表里面id为6b6c11c537的数据，address内容更新为“北京朝阳”
        if (yn)
            comment.setComment_status("2");
        else comment.setComment_status("3");

        comment.update(comment.getObjectId(), new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(context,"成功",Toast.LENGTH_SHORT).show();
                    notifyItemChanged(position,comment);
                } else {
                    Toast.makeText(context,"失败"  + e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }

        });
    }

    @Override
    public int getItemCount() {
        return myData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_comment_student)
        TextView tvCommentStudent;
        @BindView(R.id.tv_comment_level)
        TextView tvCommentLevel;
        @BindView(R.id.tv_comment_employee)
        TextView tvCommentEmployee;
        @BindView(R.id.tv_comment_details)
        TextView tvCommentDetails;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_comment_status)
        TextView tvCommentStatus;
        @BindView(R.id.tv_no)
        TextView tvNo;
        @BindView(R.id.tv_yes)
        TextView tvYes;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
